package com.example.liwushuo;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONObject;

import com.example.liwushuo.adapter.HorTopAllAdapter;
import com.example.liwushuo.bean.BaseBean;
import com.example.liwushuo.bean.Collections;
import com.example.liwushuo.urls.UrlConfig;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

public class TopAllActivity extends ActionBarActivity {

	private ListView listView;
	private List<Collections> list;
	private HorTopAllAdapter adapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_top_all);
		// getSupportFragmentManager().beginTransaction().replace(R.id.all_horinfo,
		// new AllInfoBar()).commit();
		initView();
		initData();
	}

	private void initData() {
		// TODO Auto-generated method stub
		HttpUtils httpUtils = new HttpUtils();
		httpUtils.send(HttpMethod.GET, UrlConfig.URL_HOR_TOPALL,
				new RequestCallBack<String>() {

					@Override
					public void onFailure(HttpException arg0, String arg1) {
						// TODO Auto-generated method stub

					}

					@Override
					public void onSuccess(ResponseInfo<String> responseInfo) {
						// TODO Auto-generated method stub
						String json = responseInfo.result;
						BaseBean bean = com.alibaba.fastjson.JSONObject
								.parseObject(json, BaseBean.class);
						List<Collections> list2 = bean.getData()
								.getCollections();
						list.addAll(list2);

					}
				});
	}

	private void initView() {
		// TODO Auto-generated method stub
		listView = (ListView) findViewById(R.id.topall_id);
		list = new ArrayList<Collections>();
		adapter = new HorTopAllAdapter(list, getApplicationContext());
		listView.setAdapter(adapter);
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				int id = list.get(arg2).getId();
				Intent intent = new Intent(getApplicationContext(),
						TopAllInfoActivity.class);
				intent.putExtra("id", id);

				startActivity(intent);
			}
		});
	}

}
